from flask import Blueprint, request
from flask_jwt_extended import jwt_required, get_jwt_identity

from service.article_service import add_article, get_article, delete_article, update_article
from utils import response

article_bp = Blueprint("article", __name__, url_prefix="/article")
from log import logger

@article_bp.route("/addArticle", methods=['POST'])
@jwt_required()
def add_article_router():
    body = request.get_json()
    try:
        title = body["title"]
        content = body["content"]
        cover_img = body["cover_img"]
        state = body["state"]
        category_id = body["category_id"]
    except Exception as ex:
        return response(400, f"缺少参数{ex}", None)

    sub = get_jwt_identity()

    try:
        add_article(user_id=sub['user_id'],
                    username=sub['username'],
                    title=title,
                    content=content,
                    cover_img=cover_img,
                    state=state,
                    category_id=category_id)

        logger.info(f"文章({title})创建成功")
        return response(200, "文章创建成功", None)
    except Exception as ex:
        logger.error(f"文章({title})创建失败{ex}")
        return response(500, str(ex), None)


@article_bp.route("/getArticle", methods=['GET'])
def get_article_router():
    body = request.get_json()

    category_id = body.get("category_id")
    title = body.get("title")
    state = body.get("state")
    create_user_name = body.get("create_user_name")

    page = body.get("page",1)
    size = body.get("size",5)

    try:
        data = get_article(category_id=category_id,
                           title=title,
                           state=state,
                           create_user_name=create_user_name,
                           page=page,
                           size=size)
        logger.info(f"文章列表获取成功")
        return response(200, "查询文章成功", data)
    except Exception as ex:
        logger.error(f"文章列表获取失败{ex}")
        return response(500, str(ex), None)


@article_bp.route("/updateArticle", methods=['PUT'])
def update_article_router():
    body = request.get_json()
    try:
        article_id = body["article_id"]
    except Exception as ex:
        return response(400, f"缺少参数{ex}", None)

    title = body.get("title")
    content = body.get("content")
    cover_img = body.get("cover_img")
    state = body.get("state")
    category_id = body.get("category_id")

    try:
        update_article(article_id=article_id,
                       title=title,
                       content=content,
                       cover_img=cover_img,
                       state=state,
                       category_id=category_id)

        logger.info(f"文章({title})修改成功")
        return response(200, "文章更新成功", None)
    except Exception as ex:
        logger.error(f"文章({title})修改失败{ex}")
        return response(500, str(ex), None)


@article_bp.route("/deleteArticle", methods=['DELETE'])
def delete_article_router():
    body = request.get_json()
    try:
        article_id = body["article_id"]
    except Exception as ex:
        return response(400, f"缺少参数{ex}", None)

    try:
        delete_article(article_id=article_id)
        logger.info(f"文章({article_id})删除成功")
        return response(200, "文章删除成功", None)
    except Exception as ex:
        logger.error(f"文章({article_id})删除失败")
        return response(500, str(ex), None)
